using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using System.Collections.Specialized;

namespace AURP.SP.ListEventReceiver.Features.AURPAutoIncrementCounter
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("480145a6-b491-4bbf-8570-586f395b27f3")]
    public class AURPAutoIncrementCounterEventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            try
            {
                SPSite site = (SPSite)properties.Feature.Parent;
                SPWeb web = site.OpenWeb();
                //SPWeb web = (SPWeb)properties.Feature.Parent;
                /*=============== list ===============*/
                SPList list = web.Lists.TryGetList("Auto Increment Counter");
                if (list == null)
                {
                    Guid guid = web.Lists.Add("Auto Increment Counter", "Auto Increment Counter", SPListTemplateType.GenericList);
                    SPList counterList = web.Lists[guid];

                    counterList.ContentTypesEnabled = true;
                    counterList.Update();
                    
                    SPContentType officeCType = web.ContentTypes["List Counter"];
                    counterList.ContentTypes.Add(officeCType);

                    /* ======== Remove Item Content Type  ======== */
                    if (counterList.ContentTypes["Item"].Id != null)
                    {
                        counterList.ContentTypes.Delete(counterList.ContentTypes["Item"].Id);
                        counterList.Update();
                    }

                    /* ======== Part I (create a view) ======== */

                    // add the field names to the string collection
                    StringCollection strViewFields = new StringCollection();
                    strViewFields.Add("ListGUID");
                    strViewFields.Add("LinkToList");
                    strViewFields.Add("Prefix");
                    strViewFields.Add("DefaultCount");
                    strViewFields.Add("CurrentCount");
                    strViewFields.Add("Remarks");

                    // create a standard view with the set of fields defined in the collection
                    counterList.Views.Add("Admin View", strViewFields, String.Empty, 10, true, false, SPViewCollection.SPViewType.Html, false);                    
                    /* ==== End Part I ==== */

                    /* ==== Set Default View ==== */
                    SPView view = counterList.Views["Admin View"];
                    view.DefaultView = true;
                    view.Update();

                    /* ==== Hide Title field ==== */
                    SPField titleField = counterList.Fields.GetFieldByInternalName("Title");
                    titleField.Hidden = true;
                    titleField.Update();
                    counterList.Update();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }


        // Uncomment the method below to handle the event raised before a feature is deactivated.

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            try
            {
                SPSite site = (SPSite)properties.Feature.Parent;
                SPWeb web = site.OpenWeb();
                //SPWeb web = (SPWeb)properties.Feature.Parent;
                /*=============== list ===============*/
                SPList counterList = web.Lists.TryGetList("Auto Increment Counter");
                if (counterList != null)
                {
                    /* ======== Remove counterList ======== */
                    web.Lists.Delete(counterList.ID);
                }

                ///* ======== Remove counterList Content Type & Columns ======== */
                //web.ContentTypes.Delete(web.ContentTypes["List Counter"].Id);

                //web.Fields.Delete("ListGUID");
                //web.Fields.Delete("LinkToList");
                //web.Fields.Delete("Prefix");
                //web.Fields.Delete("DefaultCount");
                //web.Fields.Delete("CurrentCount");
                //web.Fields.Delete("Remarks");

            }
            catch (Exception ex)
            {
                throw ex;
            }

        }


        // Uncomment the method below to handle the event raised after a feature has been installed.

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
        }


        // Uncomment the method below to handle the event raised before a feature is uninstalled.

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
        }

        // Uncomment the method below to handle the event raised when a feature is upgrading.

        public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        {
        }
    }
}
